package com.ronghui.cash.service.impl;

import com.ronghui.cash.dto.ReturnFenqiBillBean;
import com.ronghui.cash.entity.FenqiBill;
import com.ronghui.cash.entity.Orders;
import com.ronghui.cash.mapper.OrdersMapper;
import com.ronghui.cash.service.OrdersFenqiService;
import com.ronghui.cash.service.OrdersService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ronghui.cash.util.DateUtil;
import com.ronghui.cash.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author wiki
 * @since 2017-12-04
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    OrdersFenqiService ordersFenqiService;

    @Override
    public List<ReturnFenqiBillBean> getCurrentFenqiBill(Long userId) {
        return getFenqiBill(userId, "yes");// yes表示取当前月数据
    }

    @Override
    public List<FenqiBill> getMyAllFenqiBill(Long userId) {
        return ordersFenqiService.selectFenqiBill(userId, null);
    }


    public List<ReturnFenqiBillBean> getFenqiBill(Long userId, String currentMonth) {
        List<FenqiBill> FenqiBills = ordersFenqiService.selectFenqiBill(userId, currentMonth);

        List<ReturnFenqiBillBean> ReturnFenqiBillBeans = new ArrayList<ReturnFenqiBillBean>();

        for (FenqiBill fb : FenqiBills) {

            Double money = fb.getMoney();//应还
            Double penalty = fb.getOverdueMoney();//违约金
            Double returnMoney = Util.add(money, penalty);//还款金额（应还+违约金）
            String returnDate = fb.getDate();//还款日期
            int calcDate = DateUtil.clacDate(returnDate);
            String date = "";
            String overdue = "2";// 是否逾期 1正常，2逾期

			/*if (calcDate == 0) {
				date = "今天还款";
				overdue = "1";
			} else if (calcDate > 0) {
				date = "剩余" + calcDate + "天";
				overdue = "1";
			} else {
				date = "逾期" + fb.getOverdueDay() + "天";
			}*/

            if (calcDate >= 0) {
                overdue = "1";
                date = returnDate + "待还";
            } else {
                date = "逾期" + fb.getOverdueDay() + "天";
            }

            ReturnFenqiBillBean rf = new ReturnFenqiBillBean();
            rf.setDate(date);
            rf.setAllPeriods(fb.getAllPeriods());
            rf.setMoney(Util.double2Double(returnMoney));//还款金额（应还+违约金）
            rf.setOrderFenqiId(fb.getOrderFenqiId());
            rf.setOverdue(overdue);// 是否逾期 1正常，2逾期
            rf.setOrderId(fb.getOrderId());
            rf.setPenalty(Util.double2Double(penalty));//违约金
            rf.setPeriods(fb.getPeriods());

            ReturnFenqiBillBeans.add(rf);
        }

        return ReturnFenqiBillBeans;
    }
}
